package LiaoTianYouxi;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
//out 和 ois 怎么协调或是能删吗

/*开发日志
    /**
 * 首先我需要关键字
 * 其次需要两个集合存放输出流和关键字
 * 需要两个任务
 * 第一多客户链接
 * 第二输出和读入之后打印
 目前对输入输出流，即文件和客户端和输出端的之间留的关系（）；
 */

public class TestSever {
    private static ServerSocket ss;
    private static List<Socket> clients = new ArrayList<>();
    private static List<DataOutputStream> outs = new ArrayList<>();

    public static void main(String[] args) throws IOException {
        System.out.println("hello，服务器启动");
        ss = new ServerSocket(6888);

        try {
            while (true) {
                Socket s = ss.accept();
                clients.add(s);
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                outs.add(dos);
                System.out.println("加一个服务器");
                new Thread(() -> {
                    try {
                        handleClient(s);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ss != null) {
                    ss.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void transfile(List list2) throws IOException {
        FileOutputStream fosForDel = new FileOutputStream("D:\\data.dat");
        ObjectOutputStream oosForDel = new ObjectOutputStream(fosForDel);
        try {
            oosForDel.writeObject(list2);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            oosForDel.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        try {
            fosForDel.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
        public static void handleClient(Socket s) throws IOException {
            try (ObjectInputStream ois = new ObjectInputStream(s.getInputStream())) {
                FileOutputStream fos = new FileOutputStream("D:\\data.dat");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                // 2.传入信息
                //告诉客户端已经收到了：
                Test test = new Test();
                while (true) {
                    Object obj = ois.readObject();
                    if (obj instanceof String) {
                        String sor = (String) obj;
                        System.out.println("客户端说: " + sor);
                        //图书菜单
                        if (sor.equals("传递图书信息")) {
                            try {
                                for (DataOutputStream out : outs) {
                                    oos.writeObject(test.menu());
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (sor.equals("1")) {
                                    int a = 1;
                                    oos.writeObject(test.getMenu1(a));
                        } else if (sor.equals("2")) {
                                oos.writeObject("请输入图书信息：");
                                //将传入的对象写进文件，这里需要追加，需要追加流
                        } else if (sor.equals("3")) {
                                oos.writeObject("请输入图书序号:");
                                transfile(test.translist());
                            //怎么去得到被修改的列表{类吗}
                        } else if (sor.equals("4")) {
                                int a = 4;
                                oos.writeObject(test.getMenu1(a));
                        } else {
                                oos.writeObject(sor);
                            }
                        }
                    else
                        oos.writeObject(obj);//写进文件
                }
            } catch(IOException e){
                System.out.println("客户端断开连接：" + s.getInetAddress());
                clients.remove(s);
                //outputs.removeIf引入的一种集合操作方法，用于根据给定的条件删除集合中的元素。
                outs.removeIf(output -> output.toString().contains(s.toString()));
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } finally{
                if(s!=null){
                    s.close();
                }
            }
        }
}
